﻿using AvaloniaCommunityMvvmTemplate.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AvaloniaCommunityMvvmTemplate.ViewModels
{
    public partial class MainWindowViewModel
    {
        [ObservableProperty]
        private ObservableCollection<DirectoryTreeNode> folderItems
            = new ObservableCollection<DirectoryTreeNode>();

        [RelayCommand]
        private void SearchFolderCommand()
        {
            FolderItems.Clear();
            //FolderItems = GetFolderNodes("根目录完整路径");
            FolderItems = GetFolderNodes(@"R:\NOTE\");
        }


        
        private ObservableCollection<DirectoryTreeNode> GetFolderNodes(string path)
        {
            ObservableCollection<DirectoryTreeNode> _folderNodes = new ObservableCollection<DirectoryTreeNode>();
            if (!Directory.Exists(path))
            {
                return _folderNodes;
            }
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            DirectoryInfo[] dirs = dirInfo.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                DirectoryTreeNode node = new DirectoryTreeNode();
                node.NodeName = dir.Name;
                node.Tag = dir.FullName;
                node.Children = GetFolderNodes(dir.FullName);
                node.FolderNum=node.Children.Count;
                FileInfo[] files = dirInfo.GetFiles();
                node.FileNum= files.Length;
                _folderNodes.Add(node);
            }
            return _folderNodes;
        }



    }
}
